<template>
  <div>
    <!-- 悬浮窗 -->
    <div class="message-float" @click="showDialog" v-if="hasNewMessage">
      <i class="el-icon-bell"></i>
      <span class="message-badge" v-if="unreadCount > 0">{{ unreadCount }}</span>
    </div>

    <!-- 消息弹窗 -->
    <el-dialog title="消息通知" :visible.sync="visible" width="30%" :before-close="handleClose">
      <div class="message-list" v-if="messages.length > 0">
        <div v-for="(msg, index) in messages" :key="index" class="message-item">
          <div class="message-title">{{ msg.title }}</div>
          <div class="message-content">{{ msg.context }}</div>
          <div class="message-time">{{ msg.time }}</div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="clearMessages">清空消息</el-button>
        <el-button @click="handleClose">关 闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'MessageDialog',
  data() {
    return {
      visible: false,
      messages: [],
      hasNewMessage: false,
      unreadCount: 0
    }
  },
  methods: {
    // 添加新消息
    addMessage(title, context) {
      const message = {
        title,
        context,
        time: new Date().toLocaleString()
      };
      this.messages.unshift(message);
      this.hasNewMessage = true;
      this.unreadCount++;
    },
    // 显示弹窗
    showDialog() {
      this.visible = true;
      this.unreadCount = 0;
    },
    // 关闭弹窗
    handleClose() {
      this.visible = false;
    },
    // 清空消息
    clearMessages() {
      this.messages = [];
      this.hasNewMessage = false;
      this.unreadCount = 0;
    }
  }
}
</script>

<style scoped>
.message-float {
  position: fixed;
  right: 20px;
  top: 20px;
  width: 50px;
  height: 50px;
  background-color: #409EFF;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  z-index: 2000;
}

.message-float i {
  color: white;
  font-size: 24px;
}

.message-badge {
  position: absolute;
  top: -5px;
  right: -5px;
  background-color: #F56C6C;
  color: white;
  border-radius: 10px;
  padding: 0 6px;
  font-size: 12px;
  min-width: 18px;
  height: 18px;
  line-height: 18px;
  text-align: center;
}

.message-list {
  max-height: 400px;
  overflow-y: auto;
  padding: 0;
  margin: 0;
}

.message-item {
  padding: 15px;
  border-bottom: 1px solid #EBEEF5;
  background: transparent;
}

.message-item:last-child {
  border-bottom: none;
}

.message-title {
  font-weight: bold;
  margin-bottom: 8px;
  color: #303133;
}

.message-content {
  color: #606266;
  margin-bottom: 8px;
  line-height: 1.5;
}

.message-time {
  font-size: 12px;
  color: #909399;
}

/* 自定义弹窗样式 */
:deep(.el-dialog__body) {
  padding: 0;
}

:deep(.el-dialog__header) {
  padding: 15px 20px;
  border-bottom: 1px solid #EBEEF5;
}

:deep(.el-dialog__footer) {
  padding: 10px 20px;
  border-top: 1px solid #EBEEF5;
}
</style>